package JAVA300.第十一章多线程;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 模拟CAS机制：比较并交换
 *
 * ，模拟抢货秒杀
 */
public class thread_19_others_8CAS {


    //y秒杀数量借助CAS实现好的一个类型
    private static AtomicInteger stock = new AtomicInteger(5);

    public static void main(String[] args) {
        for (int i=0;i<5;i++){
            new Thread(()->{
                //模拟延迟
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //相减获取新数据
                Integer left = stock.decrementAndGet();
                if(left<1){
                    System.out.println("抢完啦");
                    return;
                }
                System.out.println(Thread.currentThread().getName()+"抢到啦@@@");
                System.out.println("还有"+left);
            }).start();
        }

    }

}
